package com.qf.taobei.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.taobei.entity.Comment;
import com.qf.taobei.entity.Order;
import com.qf.taobei.entity.RespBean;
import com.qf.taobei.entity.User;
import com.qf.taobei.service.CommentService;
import com.qf.taobei.service.OrderService;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 吴彦祖
 * @version 1.0
 * @description: TODO
 * @date 2022/3/4 18:53
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private OrderService orderService;

    @PostMapping ("/addComment")
    public RespBean addComment(HttpSession session,Integer pid, String context){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        Comment comment = new Comment();
        comment.setCommentContext(context);
        comment.setPid(pid);
        comment.setUid(uid);
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        comment.setCreateTime(ft.format(new Date()));
        Integer integer = commentService.addComment(comment);
        if(integer == 0){
            return RespBean.respError("删除失败");
        }
        return RespBean.respSuccess("删除成功",integer);

    }


    /**
     * 添加商品评价
     * @param comments
     * @return
     */
    @PostMapping("/addComments")
    public RespBean addComments(HttpSession session,String comments) {
        System.out.println(comments);
        List<Comment> commentList = JSON.parseArray(comments, Comment.class);
        Comment comment = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i=0;i<commentList.size();i++){
            comment = commentList.get(i);
            comment.setCreateTime(sdf.format(new Date()));
            commentService.addComment(comment);
        }
        //修改订单状态为完成状态
        Order order = new Order();
        order.setId((Integer) session.getAttribute("orderId"));
        order.setStatus(6);
        orderService.updeteOrderByOrderId(order);
        return RespBean.respSuccess("成功");
    }
}
